package Gui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.border.BevelBorder;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPathExpressionException;

import org.xml.sax.SAXException;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.IOException;

import Database.*;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;

public class UpdateDialog extends JDialog {

	private final JPanel contentPanel = new JPanel();
	private JTextField tfSearch;
	private JTextField tfRqDe;
	private JTextField tfRqLetters;
	private JTextField tfRqNumber;
	private JTextField tfRqDate;
	private JTextField tfFirstName;
	private JTextField tfLastName;
	private JTextField tfDoB;
	private JTextField tfPoB;
	private JTextField tfNationality;
	private JTextField tfAddress;
	private Wanted wanted;
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			UpdateDialog dialog = new UpdateDialog();
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the dialog.
	 */
	public UpdateDialog() {
		setBounds(100, 100, 628, 328);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(null);
		
		JLabel label = new JLabel("Input PersonID or Wanted ID");
		label.setBounds(10, 15, 203, 14);
		contentPanel.add(label);
		
		tfSearch = new JTextField();
		tfSearch.setColumns(10);
		tfSearch.setBounds(184, 12, 231, 20);
		contentPanel.add(tfSearch);
		
		JButton btnSearch = new JButton("Search");
		
		btnSearch.setBounds(425, 11, 86, 23);
		contentPanel.add(btnSearch);
		
		JLabel label_1 = new JLabel("Required Action");
		label_1.setBounds(10, 59, 110, 14);
		contentPanel.add(label_1);
		
		tfRqDe = new JTextField();
		tfRqDe.setColumns(10);
		tfRqDe.setBounds(10, 109, 265, 20);
		contentPanel.add(tfRqDe);
		
		JLabel label_2 = new JLabel("Requesting Department ");
		label_2.setBounds(10, 84, 159, 14);
		contentPanel.add(label_2);
		
		JLabel label_3 = new JLabel("Requesting department letters");
		label_3.setBounds(10, 137, 235, 14);
		contentPanel.add(label_3);
		
		tfRqLetters = new JTextField();
		tfRqLetters.setColumns(10);
		tfRqLetters.setBounds(10, 153, 265, 20);
		contentPanel.add(tfRqLetters);
		
		tfRqNumber = new JTextField();
		tfRqNumber.setColumns(10);
		tfRqNumber.setBounds(10, 193, 265, 20);
		contentPanel.add(tfRqNumber);
		
		JLabel label_4 = new JLabel("Requesting department number");
		label_4.setBounds(10, 178, 235, 14);
		contentPanel.add(label_4);
		
		tfRqDate = new JTextField();
		tfRqDate.setColumns(10);
		tfRqDate.setBounds(111, 220, 164, 20);
		contentPanel.add(tfRqDate);
		
		JLabel label_5 = new JLabel("Request Date");
		label_5.setBounds(10, 223, 86, 14);
		contentPanel.add(label_5);
		
		JPanel panel = new JPanel();
		panel.setLayout(null);
		panel.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null));
		panel.setBounds(285, 59, 313, 161);
		contentPanel.add(panel);
		
		JLabel label_6 = new JLabel("Firstname");
		label_6.setBounds(10, 11, 59, 14);
		panel.add(label_6);
		
		tfFirstName = new JTextField();
		tfFirstName.setEditable(false);
		tfFirstName.setColumns(10);
		tfFirstName.setBounds(71, 8, 78, 20);
		panel.add(tfFirstName);
		
		JLabel label_7 = new JLabel("Lastname");
		label_7.setBounds(159, 11, 59, 14);
		panel.add(label_7);
		
		tfLastName = new JTextField();
		tfLastName.setEditable(false);
		tfLastName.setColumns(10);
		tfLastName.setBounds(217, 8, 86, 20);
		panel.add(tfLastName);
		
		JLabel label_8 = new JLabel("DoB");
		label_8.setBounds(10, 36, 46, 14);
		panel.add(label_8);
		
		tfDoB = new JTextField();
		tfDoB.setEditable(false);
		tfDoB.setColumns(10);
		tfDoB.setBounds(70, 33, 78, 20);
		panel.add(tfDoB);
		
		JLabel label_9 = new JLabel("PoB");
		label_9.setBounds(10, 61, 46, 14);
		panel.add(label_9);
		
		tfPoB = new JTextField();
		tfPoB.setEditable(false);
		tfPoB.setColumns(10);
		tfPoB.setBounds(70, 58, 225, 20);
		panel.add(tfPoB);
		
		JLabel label_10 = new JLabel("Nationality");
		label_10.setBounds(10, 86, 59, 14);
		panel.add(label_10);
		
		tfNationality = new JTextField();
		tfNationality.setEditable(false);
		tfNationality.setColumns(10);
		tfNationality.setBounds(70, 83, 165, 20);
		panel.add(tfNationality);
		
		JLabel label_11 = new JLabel("Address");
		label_11.setBounds(10, 114, 59, 14);
		panel.add(label_11);
		
		tfAddress = new JTextField();
		tfAddress.setEditable(false);
		tfAddress.setColumns(10);
		tfAddress.setBounds(70, 111, 165, 20);
		panel.add(tfAddress);
		
		final JComboBox cbRAction = new JComboBox(new Object[]{});
		cbRAction.setModel(new DefaultComboBoxModel(new String[] {"Must be Arrested", "Notify to Check", "Prevent from Travelling", "Put on Probation", "Release"}));
		cbRAction.setToolTipText("");
		cbRAction.setBounds(111, 56, 147, 20);
		contentPanel.add(cbRAction);
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton btnUpdate = new JButton("Update");
				btnUpdate.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						Wanted updateWanted = new Wanted(wanted.getID(), wanted.getPersonID(),cbRAction.getSelectedIndex()+1 ,tfRqDe.getText(), tfRqLetters.getText(), tfRqLetters.getText(), tfRqDate.getText());
						boolean update = XMLProvider.updateWanted(updateWanted);
						if(update == true)
							JOptionPane.showMessageDialog(null, "Update Success!");
						else
							JOptionPane.showMessageDialog(null, "Update Fail!");
						
					}
				});
				btnUpdate.setActionCommand("OK");
				buttonPane.add(btnUpdate);
				getRootPane().setDefaultButton(btnUpdate);
			}
			{
				JButton btnCancel = new JButton("Cancel");
				btnCancel.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						setVisible(false);
					}
				});
				btnCancel.setActionCommand("Cancel");
				buttonPane.add(btnCancel);
			}
		}
		btnSearch.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {

				String id = tfSearch.getText();
				try {
					XMLProvider.LoadXML();
					wanted = XMLProvider.getWantedbyPersonID(id);
					if(wanted == null)
					{
						wanted = XMLProvider.getWantedbyWantedID(id);
					}
					if(wanted == null)
						JOptionPane.showMessageDialog(null, "Cannot find Wanted");
					else
					{
						Person person = XMLProvider.GetPersonbyID(wanted.getPersonID());
						tfAddress.setText(person.getAddress());
						tfDoB.setText(person.getDoB()); 
						tfFirstName.setText(person.getfirstName());
						tfLastName.setText(person.getlastName());
						tfNationality.setText(person.getNationality());
						tfPoB.setText(person.getPoB());
						
						
						cbRAction.setSelectedIndex(wanted.getRequireAction()-1);
						
						tfRqDe.setText(wanted.getDepartmentName());
						tfRqLetters.setText(wanted.getDepartmentLetter());
						tfRqNumber.setText(wanted.getDepartmentNumber());
						tfRqDate.setText(wanted.getRequestDate());
					}
				} catch (XPathExpressionException
						| ParserConfigurationException | SAXException
						| IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
			
			}
		});
	}
}
